﻿namespace Liger.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Liger.Web.Common;
    using Liger.Web.Common.Extensions;
    using System.ComponentModel;
    using Liger.Web.Javascript;
    using Liger.Web.Common.JSON;

    public class Tree : UIComponentBase
    {

        public Tree()
            : this(new StringBuilder())
        {

        }
        public Tree(StringBuilder writer)
            : base(writer)
        {
            this.Properties = new Dictionary<string, object>();
        }

        public string Url
        {
            get;
            set;
        }
        public object Data
        {
            get;
            set;
        }
        public bool? Checkbox
        {
            get;
            set;
        }
        public bool? AutoCheckboxEven
        {
            get;
            set;
        }
        public string ParentIcon
        {
            get;
            set;
        }
        public string ChildIcon
        {
            get;
            set;
        }
        public string TextFieldName
        {
            get;
            set;
        }
        public List<string> Attribute
        {
            get;
            set;
        }
        public bool? TreeLine
        {
            get;
            set;
        }
        public int? NodeWidth
        {
            get;
            set;
        }
        public string StatusName
        {
            get;
            set;
        }
        //是否叶节点
        public Func<string> IsLeaf
        {
            get;
            set;
        }
        /// <summary>
        /// 是否单选
        /// </summary>
        public bool? Single
        {
            get;
            set;
        }
        public string IdFieldName
        {
            get;
            set;
        }
        public string ParentIDFieldName
        {
            get;
            set;
        } 

        /// <summary>
        /// 是否以动画的形式显示
        /// </summary>
        public bool? Slide
        {
            get;
            set;
        }
        public string IconFieldName
        {
            get;
            set;
        }
        public bool? NodeDraggable
        {
            get;
            set;
        }

        public Func<string> NodeDraggingRender
        {
            get;
            set;
        }
        public bool? BtnClickToToggleOnly
        {
            get;
            set;
        }

        /// <summary>
        /// 其他可扩展的属性
        /// </summary>
        public Dictionary<string, object> Properties
        {
            get;
            private set;
        }

        /// <summary>
        /// 返回ligerTree的Manager
        /// </summary>
        public string Manager
        {
            get;
            set;
        }

        public override void WriteHTML()
        {
            if (!ID.HasValue())
                ID = "Tree" + System.Guid.NewGuid().ToString();

            string TagName = string.IsNullOrEmpty(this.TagName) ? "Tree" : this.TagName;

            Writer.Append("<{1} id='{0}'></{1}>".FormatWith(ID, TagName));

            base.WriteScript();
        }
        public override void WriteScript()
        {
            JSWriter.WriteJQueryOnLoad();
            if (this.Manager.HasValue())
            {
                JSWriter.Append("window.{0} = ".FormatWith(this.Manager));
            }
            JSWriter.WriteJQueryPlugin(ID, "ligerTree");
            JSWriter.Append(GetTreeParmJSON());
            JSWriter.WriteEndJQueryPlugin();
            JSWriter.WriteEndJQueryOnLoad();

            base.WriteScript();
        }

        public string GetTreeParmJSON()
        {
            IJavascriptObjectWriter JSWriter = new JavascriptObjectWriter();
            JSWriter.PropertyVariableCase = this.JSWriter.PropertyVariableCase;
            JSWriter.WriteOjbect();
            JSWriter.WriteProperty("Url", this.Url);
            JSWriter.WriteProperty("Data", ()=> GetObjectJSON(this.Data));
            JSWriter.WriteProperty("Checkbox", this.Checkbox);
            JSWriter.WriteProperty("AutoCheckboxEven", this.AutoCheckboxEven);
            JSWriter.WriteProperty("ParentIcon", this.ParentIcon);
            JSWriter.WriteProperty("ChildIcon", this.ChildIcon);
            JSWriter.WriteProperty("TextFieldName", this.TextFieldName);
            JSWriter.WriteProperty("Attribute", this.Attribute);
            JSWriter.WriteProperty("TreeLine", this.TreeLine);
            JSWriter.WriteProperty("NodeWidth", this.NodeWidth);
            JSWriter.WriteProperty("StatusName", this.StatusName);
            JSWriter.WriteProperty("IsLeaf", this.IsLeaf);
            JSWriter.WriteProperty("Single", this.Single);
            JSWriter.WriteProperty("IdFieldName", this.IdFieldName);
            JSWriter.WriteProperty("ParentIDFieldName", this.ParentIDFieldName); 
            JSWriter.WriteProperty("Slide", this.Slide);
            JSWriter.WriteProperty("IconFieldName", this.IconFieldName);
            JSWriter.WriteProperty("NodeDraggable", this.NodeDraggable);
            JSWriter.WriteProperty("NodeDraggingRender", this.NodeDraggingRender);
            JSWriter.WriteProperty("BtnClickToToggleOnly", this.BtnClickToToggleOnly); 
            foreach (var prop in this.Properties.Keys)
            {
                var value = this.Properties[prop];
                JSWriter.WritePropertyObject(prop, value);
            }

            foreach (var even in this.Events)
            {
                JSWriter.WriteProperty(even.Key, even.Value);
            }

            JSWriter.WriteEndOjbect();

            return JSWriter.Output();
        }
    }
}